package com.huawei.secure.android.common.encrypt.hash;

import android.text.TextUtils;
import android.util.Log;
import com.huawei.secure.android.common.encrypt.utils.a;
import com.huawei.secure.android.common.encrypt.utils.b;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

public abstract class FileSHA256 {
    private static final String TAG = "FileSHA256";
    private static final String i = "";
    private static final int p = 8192;
    private static final String q = "SHA-256";
    private static final String[] r = new String[]{"SHA-256", "SHA-384", "SHA-512"};

    public static String fileSHA256Encrypt(File file) {
        return fileSHAEncrypt(file, "SHA-256");
    }

    public static String fileSHAEncrypt(File file, String str) {
        InputStream fileInputStream;
        NoSuchAlgorithmException e;
        Throwable th;
        IOException e2;
        String str2 = null;
        Object obj = null;
        if (TextUtils.isEmpty(str) || !d(str)) {
            Log.e(TAG, "algorithm is empty or not safe");
            return "";
        } else if (a(file)) {
            try {
                MessageDigest instance = MessageDigest.getInstance(str);
                fileInputStream = new FileInputStream(file);
                try {
                    byte[] bArr = new byte[p];
                    while (true) {
                        int read = fileInputStream.read(bArr);
                        if (read <= 0) {
                            break;
                        }
                        instance.update(bArr, 0, read);
                        obj = 1;
                    }
                    if (obj != null) {
                        str2 = a.f(instance.digest());
                    }
                    b.a(fileInputStream);
                    return str2;
                } catch (NoSuchAlgorithmException e3) {
                    e = e3;
                    try {
                        Log.e(TAG, "NoSuchAlgorithmException" + e.getMessage());
                        b.a(fileInputStream);
                        return null;
                    } catch (Throwable th2) {
                        th = th2;
                        b.a(fileInputStream);
                        throw th;
                    }
                } catch (IOException e4) {
                    e2 = e4;
                    Log.e(TAG, "IOException" + e2.getMessage());
                    b.a(fileInputStream);
                    return null;
                }
            } catch (NoSuchAlgorithmException e5) {
                e = e5;
                fileInputStream = null;
                Log.e(TAG, "NoSuchAlgorithmException" + e.getMessage());
                b.a(fileInputStream);
                return null;
            } catch (IOException e6) {
                e2 = e6;
                fileInputStream = null;
                Log.e(TAG, "IOException" + e2.getMessage());
                b.a(fileInputStream);
                return null;
            } catch (Throwable th3) {
                fileInputStream = null;
                th = th3;
                b.a(fileInputStream);
                throw th;
            }
        } else {
            Log.e(TAG, "file is not valid");
            return "";
        }
    }

    public static String inputStreamSHA256Encrypt(InputStream inputStream) {
        if (inputStream == null) {
            return "";
        }
        return inputStreamSHAEncrypt(inputStream, "SHA-256");
    }

    public static String inputStreamSHAEncrypt(InputStream inputStream, String str) {
        if (inputStream == null) {
            return "";
        }
        byte[] bArr = new byte[p];
        try {
            MessageDigest instance = MessageDigest.getInstance(str);
            while (true) {
                int read = inputStream.read(bArr);
                if (read < 0) {
                    String f = a.f(instance.digest());
                    b.a(inputStream);
                    return f;
                } else if (read > 0) {
                    instance.update(bArr, 0, read);
                }
            }
        } catch (IOException e) {
            try {
                Log.e(TAG, "inputstraem exception");
                return "";
            } finally {
                b.a(inputStream);
            }
        } catch (NoSuchAlgorithmException e2) {
            Log.e(TAG, "inputstraem exception");
            return "";
        }
    }

    public static boolean validateFileSHA256(File file, String str) {
        if (TextUtils.isEmpty(str)) {
            return false;
        }
        return str.equals(fileSHA256Encrypt(file));
    }

    public static boolean validateFileSHA(File file, String str, String str2) {
        if (!TextUtils.isEmpty(str) && d(str2)) {
            return str.equals(fileSHAEncrypt(file, str2));
        }
        Log.e(TAG, "hash value is null || algorithm is illegal");
        return false;
    }

    public static boolean validateInputStreamSHA256(InputStream inputStream, String str) {
        if (TextUtils.isEmpty(str)) {
            return false;
        }
        return str.equals(inputStreamSHA256Encrypt(inputStream));
    }

    public static boolean validateInputStreamSHA(InputStream inputStream, String str, String str2) {
        if (!TextUtils.isEmpty(str) && d(str2)) {
            return str.equals(inputStreamSHAEncrypt(inputStream, str2));
        }
        Log.e(TAG, "hash value is null || algorithm is illegal");
        return false;
    }

    private static boolean a(File file) {
        return file != null && file.exists() && file.length() > 0;
    }

    private static boolean d(String str) {
        for (String equals : r) {
            if (equals.equals(str)) {
                return true;
            }
        }
        return false;
    }
}
